package libro.cap04.contratos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class DeptDAOImpl {

	public Collection<DeptDTO> buscarTodos() throws SQLException {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		try {

			con = UConnection.getConnection();
			String sql = "SELECT deptno, dname, loc FROM departamentos";
			pstm = con.prepareStatement(sql);
			rs = pstm.executeQuery();

			ArrayList<DeptDTO> ret = new ArrayList<DeptDTO>();
			DeptDTO dto = null;

			while (rs.next()) {

				dto = new DeptDTO();
				dto.setDeptno(rs.getInt("deptno"));
				dto.setDname(rs.getString("dname"));
				dto.setLoc(rs.getString("loc"));
				ret.add(dto);

			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		finally {
			try {
				if (rs != null)
					rs.close();
				if (pstm != null)
					pstm.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);

			}
		}

	}
}
